package com.huitone.gddw.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.Role;
import com.huitone.gddw.dao.entity.RoleMenu;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.dao.entity.UserRole;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.system.RoleDTO;
import com.huitone.gddw.service.system.IRoleMenuService;
import com.huitone.gddw.service.system.IRoleService;
import com.huitone.gddw.service.system.IUserRoleService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 角色管理 控制器
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="role")
public class RoleController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(RoleController.class);
	
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private IRoleMenuService roleMenuService;
	
	@Autowired
	private IUserRoleService userRoleService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<RoleDTO> query(QueryPage<Role> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询角色管理列表";
		QueryPage<RoleDTO> result = null;
		Map<String, Object> params = new HashMap<String, Object>();
		try {
			String roleName = request.getParameter("roleName");
			params.put("roleName", roleName);
			page.setParams(params);
			result = roleService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询角色管理信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 获取角色
	 * @return List<Role>
	 */
	@ResponseBody
	@RequestMapping(value="getAll")
	public List<Role> getAll() {
		try {
			return roleService.getAll();
		} catch (Exception e) {
			LOGGER.error("查询角色信息出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 修改页面数据准备
	 * @param roleId 角色ID
	 * @return RoleDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public RoleDTO toUpdate(String roleId) {
		try {
			RoleDTO entity = roleService.toUpdate(roleId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 更改状态
	 * @param roleId 角色ID
	 * @param status 更改后状态
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="changeStatus")
	public Message changeStatus(String roleId, String status) {
		try {
			Role entity = roleService.getById(roleId);
			entity.setStatus(status);
			roleService.changeStatus(entity);
			return new Message(true, "操作成功！");
		} catch (Exception e) {
			LOGGER.error("更改角色管理状态过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "操作失败！");
		}
	}
	
	/**
	 * 保存
	 * @param roleDTO RoleDTO
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(RoleDTO roleDTO) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加角色信息";
		boolean isSuccess = true;
		String msg;
		try {
			roleService.save(roleDTO);
			msg = "保存成功！";
		} catch (Exception e) {
			LOGGER.error("保存角色管理信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 更新
	 * @param roleDTO RoleDTO
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(RoleDTO roleDTO) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改角色信息";
		boolean isSuccess = true;
		String msg;
		try {
			roleService.update(roleDTO);
			msg = "修改成功！";
		} catch (Exception e) {
			LOGGER.error("修改角色管理信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 删除
	 * @param roleId 角色ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String roleId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除角色信息";
		try {
//			List<UserRole> list = userRoleService.getByRoleId(roleId);
//			if (!list.isEmpty() || list.size() != 0) {
//				super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
//				return new Message(false, "该角色已被用户引用，无法删除！");
//			}
			roleService.delete(roleId);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "删除成功！");
		} catch (Exception e) {
			LOGGER.error("删除角色管理信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "删除失败！");
		}
	}
	
	/**
	 * 获取角色所有菜单
	 * @param roleId 角色ID
	 * @return List
	 */
	@ResponseBody
	@RequestMapping(value="getRoleMenus")
	public List<RoleMenu> getRoleMenus(String roleId) {
		try {
			List<RoleMenu> list = roleMenuService.getByRoleId(roleId);
			return list;
		} catch (Exception e) {
			LOGGER.error("获取角色所有菜单信息过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 授权角色菜单权限和数据访问权限
	 * @param roleId 角色ID
	 * @param menuIds 多个菜单ID
	 * @param acsFlag 数据访问权限
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="changeMenusAndAcsFlag")
	public Message changeMenusAndAcsFlag(String roleId, String menuIds, String acsFlag) {
		try {
			roleService.changeMenusAndAcsFlag(roleId, menuIds, acsFlag);
			return new Message(true, "授权成功！");
		} catch (Exception e) {
			LOGGER.error("授权角色菜单过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "授权失败！");
		}
	}
	
}
